package com.sunricher.telinkblemesh.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.sunricher.telinkblemesh.R;
import com.sunricher.telinkblemesh.adapter.DaliDevicesAdapter;
import com.sunricher.telinkblemeshlib.db.UartDaliDeviceManager;
import com.sunricher.telinkblemeshlib.models.UartDaliDevice;
import com.sunricher.telinkblemeshlib.models.UartDaliManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class DaliDevicesActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DaliDevicesAdapter adapter;
    private int gatewayAddress = 0;
    private boolean isDiscovering = false;

    private UartDaliManager.Callback callback = new UartDaliManager.Callback() {

        @Override
        public void didExecuteCommandOK(int daliAddress, int gatewayAddress, int cmdType, int cmd) {
            System.out.println("didExecuteCommandOK gw" + gatewayAddress + ", daliAddr " + daliAddress);
        }

        @Override
        public void didExecuteCommandFailed(int daliAddress, int gatewayAddress, int reason, int cmdType, int cmd) {
            System.out.println("didExecuteCommandFailed gw" + gatewayAddress + ", daliAddr " + daliAddress + ", " + reason + ", " + cmdType + ", " + cmd);
        }

        @Override
        public void didUpdateDeviceList(List<UartDaliDevice> devices, int gatewayAddress) {
            if (DaliDevicesActivity.this.gatewayAddress != gatewayAddress) {
                return;
            }
            DaliDevicesActivity.this.adapter.updateDevices(devices);
        }

        @Override
        public void didDiscoverEnd(int gatewayAddress, int reason) {
            isDiscovering = false;
            System.out.println("didDiscoverEnd gw " + gatewayAddress + ", reason " + reason);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dali_devices);

        gatewayAddress = getIntent().getIntExtra("gatewayAddress", 0);

        Button discoverBtn = findViewById(R.id.discover_btn);
        Button stopBtn = findViewById(R.id.stop_btn);
        discoverBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDiscovering) { return; }
                adapter.updateDevices(new ArrayList<>());
                isDiscovering = true;
                UartDaliManager.getInstance().discoverDevices(gatewayAddress, DaliDevicesActivity.this);
            }
        });
        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().stopDiscoverDevices(gatewayAddress);
            }
        });

        recyclerView = findViewById(R.id.recycler_view);

        adapter = new DaliDevicesAdapter();
        adapter.setClickListener(new DaliDevicesAdapter.OnClickListener() {
            @Override
            public void onItemClick(DaliDevicesAdapter.ViewHolder holder, int position, UartDaliDevice device) {
                if (isDiscovering) { return; }
                if (device.getDataPoints().containsKey("ON_OFF")) {
                    boolean isOn = (boolean) device.getDataPoints().get("ON_OFF");
                    device.getDataPoints().put("ON_OFF", !isOn);
                    HashMap<String, Object> onOffValue = new HashMap<>();
                    onOffValue.put("ON_OFF", !isOn);
                    UartDaliManager.getInstance().updateDataPoints(device.getGatewayAddress(), device.getDaliAddress(), onOffValue);
                }
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

         adapter.updateDevices(UartDaliDeviceManager.getInstance(this).getDevices(gatewayAddress));

        UartDaliManager.getInstance().setCallback(callback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        UartDaliManager.getInstance().setCallback(null);
    }
}